//
//  MyInfoViewController.swift
//  globalcarbusiness
//
//  Created by dongzhenyu on 2017/8/19.
//  Copyright © 2017年 dongzhenyu. All rights reserved.
//

import UIKit

class MyInfoViewController: UIViewController {

    fileprivate lazy var tableView: UITableView = {
        let table = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH), style: .grouped)
        table.delegate = self
        table.dataSource = self
        table.backgroundColor = UIColor.colorWithHexString("f7f7f7")
        return table
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(tableView)
        title = "个人信息"
    }
}


extension MyInfoViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellID = "cellID"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellID)
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: cellID)
        }
        cell?.textLabel?.text = indexPath.row == 0 ? "修改资料" : "查看信息"
        cell?.accessoryType = .disclosureIndicator
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch indexPath.row {
        case 0:
            let registerVc = RegisterViewController()
            navigationController?.pushViewController(registerVc, animated: true)
        case 1:
            setupClickEvent()
        default:
            return
        }
    }
    
    fileprivate func setupClickEvent() {
        
        showLoading("获取数据中...", isWhiteBackground: false, isCovered: false)
        NetworkTools.shareInstance.requestData(Base_url + Check_Info_url, .get, [:]) { (response, isSuccess) in
            
            switch isSuccess {
                case false:
                    self.hideLoading()
                    self.showHUD(.failure, text: "网络错误 请重试")
                case true:
                    self.hideLoading()
                
                    guard let dict = response as? [String: AnyObject] else {return}
                    guard let details = dict["storeDetailes"] as? NSDictionary else {return}
                    
                    let messageArray = NSMutableArray()
                    if let obj = details["store_name"] {
                        messageArray.add(obj)
                    } else {
                        messageArray.add("")
                    }
                    if let obj = details["address"] {
                        messageArray.add(obj)
                    } else {
                        messageArray.add("")
                    }
                    if let obj = details["bank_name"] {
                        messageArray.add(obj)
                    } else {
                        messageArray.add("")
                    }
                    if let obj = details["bank_account"] {
                        messageArray.add(obj)
                    } else {
                        messageArray.add("")
                    }
                    if let obj = details["contact"] {
                        messageArray.add(obj)
                    } else {
                        messageArray.add("")
                    }
                    
                    if let value = details["contact2"] {
                        
                        if value is NSNull {
                            messageArray.add("")
                        } else {
                            let v = value as! String
                            messageArray.add(v)
                        }
                        
                    } else {
                        messageArray.add("")
                    }
                    
                    messageArray.add(details["license_code"] ?? 0)

                    var i = 0
                    var image1 : UIImage?
                    if let str = details["license_img_url"] {
                        let url = NSURL(string: Base_url + (str as! String))
                        let imagedata = NSData(contentsOf: url! as URL)
                        if imagedata == nil {
                            i += 1
                        }
                        image1 = imagedata != nil ? UIImage(data: imagedata! as Data) : nil
                    }

                    var image2 : UIImage?
                    if let str2 = details["avatar_img_url"] {
                        let url2 = NSURL(string: Base_url + (str2 as! String))
                        let imagedata2 = NSData(contentsOf: url2! as URL)
                        if imagedata2 == nil {
                            i += 1
                        }
                        image2 = imagedata2 != nil ? UIImage(data: imagedata2! as Data) : nil
                    }
                    
                    let imageArray = NSMutableArray()
                    let array = dict["storeAuditImgDetailes"] as? NSArray
                    for obj in array! {
                        let tempDict = obj as! NSDictionary
                        let tempUrl = tempDict["img_url"] as! String
                        let imageUrl = NSURL(string: Base_url + tempUrl)
                        let tempData = NSData(contentsOf: imageUrl! as URL)
                        let tempImage = tempData != nil ? UIImage(data: tempData! as Data) : nil
                        if tempImage == nil {
                            i += 1
                        }
                        imageArray.add(tempImage ?? UIImage())
                    }
                    
                    let reviewVc = ReviewWeViewController()
                    if i != 0 {
                        self.showHUD(.failure, text: "图片未能正常显示 请重试!")
                    }
                    reviewVc.realArray = messageArray
                    reviewVc.pushStr = "fromInfo"
                    reviewVc.zhizhaoImage = image1
                    reviewVc.liebiaoImage = image2
                    
                    if imageArray.count < 6 {
                        reviewVc.imageArray = ["","","","","",""]
                        for i in 0..<imageArray.count {
                            reviewVc.imageArray?.replaceObject(at: i, with: imageArray[i])
                        }
                    } else {
                        reviewVc.imageArray = imageArray
                    }
                    
                    self.navigationController?.pushViewController(reviewVc, animated: true)
                
            }
        }
    }
    
}
